/**
 * Created by xuxa on 2017/2/26.
 */

var express = require('express');//express框架
var path=require('path');//路径
var mongoose=require('mongoose')//连接数据库
var Movie=require('./models/movie');//取到模型
var bodyParser = require('body-parser'); //表单数据格式化
var _ = require('underscore');//定义underscore
var port = process.env.POTR || 3100;//定义服务接口
var app = express();
app.locals.moment = require('moment');

mongoose.connect('mongodb://localhost/mynodejs')

app.set('views', './views/pages');//定义视图路径
app.set('view engine', 'jade');//html模版引擎
app.use(bodyParser.urlencoded()); //表单数据格式化
app.use('/static', express.static('public'));//定义静态内容
app.listen(port);//监听接口
console.log('mynodejs start on port' + port);

//index page
app.get('/', function (req, res) {
    //调模型
    Movie.fetch(function (err, movies) {
        if(err){
            console.log(err)
        }
        res.render('index', {
            title: 'mynodejs 首页',
            movies: movies
        })
    })
})
//list page
app.get('/admin/list', function (req, res) {
    Movie.fetch(function (err, movies) {
        if(err){
            console.log(err)
        }
        res.render('list', {
            title: 'mynodejs 列表页',
            movies: movies
        })
    })
})
//detail page
app.get('/movie/:id', function (req, res) {
    var id=req.params.id
    Movie.findById(id,function (err,movie) {
        res.render('detail', {
            title: 'mynodejs' + movie.title,
            movie: movie
        })
    })
})
//admin page
app.get('/admin/movie', function (req, res) {
    res.render('admin', {
        title: 'mynodejs 后台录入',
        movie: {
            title: '',
            doctor: '',
            country: '',
            year: '',
            poster: '',
            flash: '',
            summary: '',
            language: ''
        }
    })
})
//将表单post的数据存入数据库
app.post('/admin/movie/new',function (req, res) {
    var id = req.body.movie._id
    var movieObj = req.body.movie
    var _movie

    if(id!=='undefined'){
        Movie.findById(id,function (err,movie) {
            if(err){
                console.log(err)
            }
            //用新的字段替换老的字段用到underscore
            _movie = _.extend(movie,movieObj)
            _movie.save(function(err,movie){
                if(err){
                    console.log(err)
                }
                //将页面重定向
                res.redirect('/movie/' + movie._id)
            })
        })
    }
    else{

        _movie = new Movie({
            title:movieObj.title,
            doctor:movieObj.doctor,
            country:movieObj.country,
            language:movieObj.language,
            year:movieObj.year,
            poster:movieObj.poster,
            flash:movieObj.flash,
            summary:movieObj.summary
        })
        _movie.save(function (err,movie) {
            if(err){
                console.log(err)
            }
            res.redirect('/movie/'+ movie._id)
        })
    }
})

//更新电影配置路由
app.get('/admin/updata/:id',function (req,res) {
    var id=req.params.id
    if(id){
        Movie.findById(id,function (err,movie) {
            res.render('admin',{
                title: 'mynodejs 后台更新页',
                movie: movie
            })
        })
    }
})
//删除功能配置路由
app.delete('/admin/list',function (req,res) {
    var id = req.query.id

    if(id){
        Movie.remove({_id:id},function (err,movie) {
            if(err){
                console.log(err)
            }
            else{
                res.json({success:1})
            }
        })
    }

})
